package View;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.filechooser.FileNameExtensionFilter;

/** this class create the GUI in order to add new product to company catalog **/
public class NewProductToCatalogView extends JFrame {

	private static final long serialVersionUID = 42L;
	/** Required fields and attributes **/
	JLabel ProductName;
	JLabel ProductPrice;
	JLabel ProductPicture;
	JLabel ProductDescription;
	JLabel ProductID;
	JTextField IDField;
	JTextField NameField;
	JTextField PriceField;
	JTextField DescriptionField;
	JButton SubmitButton;
	JButton BackButton;
	JFileChooser chooser;
	FileNameExtensionFilter filter;
	static JButton UploadBtn;

	/** default and only constructor **/
	public NewProductToCatalogView() {
		getContentPane().setLayout(null);

		setTitle("Enter New Product");
		setSize(432,481);
		this.setLocationRelativeTo(null); 

		ProductID = new JLabel("Product ID");
		ProductID.setFont(new Font("Tahoma", Font.BOLD, 13));
		ProductID.setBounds(24, 53, 73, 14);
		getContentPane().add(ProductID);

		ProductName = new JLabel("Name");
		ProductName.setFont(new Font("Tahoma", Font.BOLD, 13));
		ProductName.setBounds(24, 81, 89, 14);
		getContentPane().add(ProductName);

		ProductPrice = new JLabel("Price");
		ProductPrice.setFont(new Font("Tahoma", Font.BOLD, 13));
		ProductPrice.setBounds(24, 111, 73, 14);
		getContentPane().add(ProductPrice);

		ProductPicture = new JLabel("Picture");
		ProductPicture.setFont(new Font("Tahoma", Font.BOLD, 13));
		ProductPicture.setBounds(24, 147, 46, 14);
		getContentPane().add(ProductPicture);

		ProductDescription = new JLabel("Description");
		ProductDescription.setFont(new Font("Tahoma", Font.BOLD, 13));
		ProductDescription.setBounds(24, 182, 89, 14);
		getContentPane().add(ProductDescription);

		IDField = new JTextField();
		IDField.setBounds(129, 51, 168, 20);
		getContentPane().add(IDField);
		IDField.setColumns(10);

		NameField = new JTextField();
		NameField.setBounds(129, 79, 168, 20);
		getContentPane().add(NameField);
		NameField.setColumns(10);

		PriceField = new JTextField();
		PriceField.setBounds(129, 109, 168, 20);
		getContentPane().add(PriceField);
		PriceField.setColumns(10);

		DescriptionField = new JTextField();
		DescriptionField.setBounds(115, 182, 238, 211);
		getContentPane().add(DescriptionField);
		DescriptionField.setColumns(10);

		SubmitButton = new JButton("Add");
		SubmitButton.setFont(new Font("Tahoma", Font.BOLD, 13));
		SubmitButton.setBounds(235, 404, 118, 28);
		getContentPane().add(SubmitButton);

		BackButton = new JButton("Back");
		BackButton.setFont(new Font("Tahoma", Font.BOLD, 13));
		BackButton.setBounds(80, 404, 112, 28);
		getContentPane().add(BackButton);

		UploadBtn = new JButton("Upload..");
		UploadBtn.setFont(new Font("Tahoma", Font.BOLD, 13));
		UploadBtn.setBounds(129, 144, 147, 27);
		getContentPane().add(UploadBtn);

	}



	/** show and hide this window functions **/
	public void showWindow(){
		setVisible(true);
	}
	public void hideWindow(){
		setVisible(false);
	}	

	/** get text fields functions **/
	public String getProductID(){
		return IDField.getText();
	}	
	public String getProductName(){
		return NameField.getText();
	}	
	public float getProductPrice(){
		if(! isFloat(PriceField.getText().toString()))
		{
			return 0;
		}
		return Float.parseFloat(PriceField.getText());
	}
	public String getProductDescription(){
		return DescriptionField.getText();
	}


	/** get buttons functions **/
	public  JButton getSubmitButton() {
		return SubmitButton;
	}
	public  JButton getBackBtn() {
		return BackButton;
	}
	public static  JButton getUploadBtn() {
		return UploadBtn;
	}

	/** clear all fields in order to maintain another insert **/
	public void clearFields()
	{
		IDField.setText(null);
		NameField.setText(null);
		PriceField.setText(null);
		DescriptionField.setText(null);
	}

	/**
	 * add listeners to submit and back button
	 * @param actionListener
	 */
	public void addNewProductToCatalogListener(ActionListener actionListener)
	{
		SubmitButton.addActionListener(actionListener);
		BackButton.addActionListener(actionListener);


	}
	/**
	 * add listener to the upload picture button
	 * @param action
	 */
	public void addupload(ActionListener action)
	{
		UploadBtn.addActionListener(action);
	}

	/** convert from string to float and check the value **/
	boolean isFloat(String str) {
		try {
			Float.parseFloat(str);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

}


